package cn.miketsu.util;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 王泽风
 * @date 2021/11/13
 */
public class HttpUtils {
    //private static Logger logger = Logger.getLogger(HttpUtil.class);

    /**
     * get请求
     *
     * @return
     */
    public static String doGet(String url) {
        try {
            HttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            request.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 HBPC/11.0.7.301");
            HttpResponse response = client.execute(request);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity(), "UTF-8");

                return strResult;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get下载
     *
     * @return
     */
    public static void doGetDownload(String url, String fielName) throws Exception {
        HttpClient client = new DefaultHttpClient();
        //发送get请求
        HttpGet request = new HttpGet(url);
        request.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 HBPC/11.0.7.301");
        HttpResponse response = client.execute(request);

        /**请求发送成功，并得到响应**/
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            InputStream content = response.getEntity().getContent();
            FileOutputStream fos = null;
            try {
                String parent = ConfigUtils.getProperties("filePath");
                File path = new File(parent);
                if (!path.exists()) {
                    path.mkdirs();
                }
                if (!path.isDirectory()) {
                    throw new Exception("配置项filePath应为目录！");
                }
                File file = new File(parent, fielName);
                if (file.exists()) {
                    System.out.println(fielName + "已存在！");
                    return;
                }
                fos = new FileOutputStream(file);
                byte[] rbody = new byte[1024];
                int readNum;
                while ((readNum = content.read(rbody)) != -1) {
                    fos.write(rbody, 0, readNum);
                }
                System.out.println("成功下载" + fielName + "！");
            } finally {
                if (content != null) {
                    content.close();
                }
                if (fos != null) {
                    fos.close();
                }
            }
        }
    }
}
